﻿Imports System.Data
Imports ADOEntityKYC
Imports System.Xml
Imports ADOEntity

Partial Class KYC_AMLListBiz
    Inherits System.Web.UI.Page

#Region " Variable Gobal "
    Dim ds As DataSet
    Dim clsAMLList As clsAMLList = New clsAMLList
    Dim TranType As String
    Dim hdfUniqueID As String
    Dim uniqueID1 As String
    Dim BizType As String
    Dim PrefixTH As String
    Dim NameTh1 As String
    Dim NameEn1 As String
    Dim Nationality As String
    Dim Business1 As String
    Dim BizOther1 As String
    Dim Des1 As String
    Dim Business2 As String
    Dim BizOther2 As String
    Dim Des2 As String
    Dim Business3 As String
    Dim BizOther3 As String
    Dim Des3 As String
    Dim IncomeSource1 As String
    Dim IncomeSource2 As String
    Dim IncomeSource3 As String
    Dim clsCheckLanguage As clsCheckLanguage
    Dim ListEntity As ListEntities = New ListEntities
    Dim clsDataTable As New clsLINQToDataTable
    Dim clsCheckID As clsCheckID
    Dim convertDate As clsDateFormat

    Dim chkEnable As Integer

    Dim chkRegister1 As String
    Dim chkRegister2 As String
    Dim chkRegister3 As String

    Dim chkNGO1 As String
    Dim chkNGO2 As String

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            ds = Session("ds")
            clsAMLList = Session("clsAMLList")

            QueryString()

            If Not IsPostBack Then
                If checkBeforeCompile() = False Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.close();", True)
                Else
                    AutoInput()
                    CreateControlMP()
                    QuerySanction()

                    If ds.Tables("Result").Rows.Count = 0 Then
                        lblMsg.Visible = True
                        rdoSanction.Visible = False
                        btnOK.Enabled = True
                    Else
                        rdoSanction.Visible = True
                        grdList.Enabled = False
                    End If

                End If

            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = ds
    End Sub

#Region "SetForm"

    Sub CreateControlMP()
        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "ตรวจสอบ List"
    End Sub

#End Region

#Region "Function"

    Private Sub QueryString()
        Try
            hdfUniqueID = Request.QueryString("hdfUniqueID").Trim
            TranType = Request.QueryString("TranType").Trim
            uniqueID1 = Request.QueryString("uniqueID").Trim
            BizType = Request.QueryString("BizType").Trim
            PrefixTH = Request.QueryString("PrefixTH").Trim
            NameTh1 = Request.QueryString("NameTh1").Trim
            NameEn1 = Request.QueryString("NameEn1").Trim
            Nationality = Request.QueryString("Nationality").Trim
            Business1 = Request.QueryString("Business1").Trim
            BizOther1 = Request.QueryString("BizOther1").Trim
            Des1 = Request.QueryString("Des1").Trim
            Business2 = Request.QueryString("Business2").Trim
            BizOther2 = Request.QueryString("BizOther2").Trim
            Des2 = Request.QueryString("Des2").Trim
            Business3 = Request.QueryString("Business3").Trim
            BizOther3 = Request.QueryString("BizOther3").Trim
            Des3 = Request.QueryString("Des3").Trim

            IncomeSource1 = Request.QueryString("IncomeSource1").Trim
            IncomeSource2 = Request.QueryString("IncomeSource2").Trim
            IncomeSource3 = Request.QueryString("IncomeSource3").Trim

            'chkEnable = Request.QueryString("chkEnable").Trim

            'chkRegister1 = Request.QueryString("chklRegis1").Trim
            'chkRegister2 = Request.QueryString("chklRegis2").Trim
            'chkRegister3 = Request.QueryString("chklRegis3").Trim
            'chkNGO1 = Request.QueryString("chklNGO1").Trim
            'chkNGO2 = Request.QueryString("chklNGO2").Trim
         
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Function checkBeforeCompile() As Boolean
        Try
            clsCheckLanguage = New clsCheckLanguage
            clsCheckID = New clsCheckID

            Dim result As Boolean = True

            If TranType = "" Then
                result = False
                GoTo chkresult
            End If

            If uniqueID1.Length = 0 Then
                result = False
                GoTo chkresult
            Else
                If Not uniqueID1 = hdfUniqueID Then
                    If clsCheckID.checkUniqueID2(uniqueID1, Session("TypeKYCBoard"), Session("CustSeq")) = False Then
                        result = False
                        clsCheckID = Nothing
                        GoTo chkresult
                    End If
                End If
            End If

            If BizType = "2" Then
                'If Not uniqueID1.Trim.Length = 13 Then
                '    result = False
                '    GoTo chkresult
                'End If
                If Not (Business1 > 51 And Business1 < 60) And Not (Business2 > 51 And Business2 < 60) And Not (Business3 > 51 And Business3 < 60) Then
                    If Not uniqueID1.Trim.Length = 13 Then
                        result = False
                        GoTo chkresult
                    End If
                End If

            ElseIf BizType = "" Then
                result = False
                GoTo chkresult
            End If

            'If PrefixTH = "" And NameTh1 = "" Then
            '    result = False
            '    GoTo chkresult
            'Else
            If PrefixTH = "" Then
                result = False
                GoTo chkresult
            End If

            If Nationality = "202" Then
                If NameTh1 = "" Then
                    result = False
                    GoTo chkresult
                End If
            End If

            'End If

            If NameEn1 = "" Then
                result = False
                GoTo chkresult
            ElseIf Not Not NameEn1 = "" Then
                If clsCheckLanguage.checkLanguageEN(NameEn1) = False Then
                    result = False
                    GoTo chkresult
                End If
            End If

            If Nationality = "" Then
                result = False
                GoTo chkresult
            End If

            If Business1 = "0" Then
                result = False
                GoTo chkresult
            ElseIf Business1 = 99 Then
                If BizOther1 = "" Then
                    result = False
                    GoTo chkresult
                End If
            End If

            'If Not Business2 = "" Then
            If Business2 = 99 Then
                If BizOther2 = "" Then
                    result = False
                    GoTo chkresult
                End If
            End If
            'End If

            'If Not Business3 = "" Then
            If Business3 = 99 Then
                If BizOther3 = "" Then
                    result = False
                    GoTo chkresult
                End If
            End If
            'End If

            If IncomeSource1 = "" Then
                result = False
                GoTo chkresult
            End If


            'If chkEnable = 1 Then
            '    If Not chkRegister1 = "1" And Not chkRegister2 = "1" And Not chkRegister3 = "1" Then

            '    Else
            '        If checkRegis() = True Then

            '        Else
            '           result = False
            '            GoTo chkresult
            '        End If
            '    End If
            'Else

            'End If

            'If chkEnable = 2 Then
            '    If chkNGO1 = "1" And chkNGO2 = "1" Then
            '         result = False
            '        GoTo chkresult
            '    Else

            '    End If
            'Else

            'End If

chkresult:
            clsCheckLanguage = Nothing

            Return result

        Catch ex As Exception
            WriteError(ex, "checkBeforeCompile")
            Throw ex
        End Try
    End Function

    Private Function checkRegis() As Boolean
        Try
            Dim result As Boolean = True
            If chkRegister1 = "1" And chkRegister2 = "1" Then
                result = False
            ElseIf chkRegister1 = "1" = True And chkRegister3 = "1" Then
                result = False
            ElseIf chkRegister2 = "1" = True And chkRegister3 = "1" Then
                result = False
            End If

            Return result
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function cutSpecialCharacter(ByVal bizName As String) As String
        Try
            Dim nameSearch As String = ""
            nameSearch = bizName.Trim.Replace(" ", "").Replace("&", "").Replace("'", "").Replace("(", "").Replace(")", "").Replace(",", "").Replace("-", "").Replace(".", "").Replace("""", "")

            Return nameSearch
        Catch ex As Exception
            Throw ex
        End Try
    End Function


    Private Sub callWSVToChkName(ByVal strFirstNameTh As String, ByVal strMidNameth As String, ByVal strLastNameTh As String, _
                                 ByVal strFirstNameEn As String, ByVal strMidNameEn As String, ByVal strLastNameEn As String)

        'Dim SanctionEntity As New SanctionEntities
        'Dim clsLINQToDataTable As New clsLINQToDataTable
        Dim intCountRowTh As Integer = 0
        Dim intCountRowEn As Integer = 0
        ' Dim intCountRowUniqueID As Integer = 0
        Dim total As Integer = 0
        Dim icount As Integer = 0
        Dim drItem As DataRow()
        Dim dsService As New DataSet

        Try
            If (ds.Tables.Contains("Result")) Then
                ds.Tables.Remove("Result")
            End If



            ' เป็นการดึง service เช็คชื่อภาษาไทย------------------------------------------------------------------------
            Dim objCallDataTH As New wsvSearchTh.ProcDQsvrsvc_th
            Dim objPrmNameTH As New wsvSearchTh.Parameters
            With objPrmNameTH
                .firstName = cutSpecialCharacter(strFirstNameTh)
                .middleName = cutSpecialCharacter(strMidNameth)
                .lastName = cutSpecialCharacter(strLastNameTh)
            End With

           Dim doc As XmlDocument = New XmlDocument

            doc.LoadXml(objCallDataTH.procDQsvrsvc_th(objPrmNameTH))
            dsService.ReadXml(New XmlNodeReader(doc))

            drItem = Nothing
            drItem = dsService.Tables("Result").Select("FirstName=''")
            icount = drItem.Length
            For i = 0 To icount - 1
                drItem(i).Delete()
            Next
            dsService.Tables("Result").AcceptChanges()

            intCountRowTh = dsService.Tables("Result").Rows.Count


            '--------------------------------------------------------------------------------------------------


            ' เป็นการดึง service เช็คชื่อภาษาอังกฤษ------------------------------------------------------------------------
            Dim objCallDataEN As New wsvSearchEn.ProcDQsvrsvc_en
            Dim objPrmNameEN As New wsvSearchEn.Parameters
            With objPrmNameEN
                .firstName = strFirstNameEn.ToUpper 'cutSpecialCharacter(strFirstNameEn.ToUpper)
                .middleName = strMidNameEn.ToUpper ' cutSpecialCharacter(strMidNameEn.ToUpper)
                .lastName = strLastNameEn.ToUpper ' cutSpecialCharacter(strLastNameEn.ToUpper)
            End With
            doc = New XmlDocument
            doc.LoadXml(objCallDataEN.procDQsvrsvc_en(objPrmNameEN))
            dsService.ReadXml(New XmlNodeReader(doc))

            drItem = Nothing
            drItem = dsService.Tables("Result").Select("FirstName=''")
            icount = drItem.Length
            For i = 0 To icount - 1
                drItem(i).Delete()
            Next
            dsService.Tables("Result").AcceptChanges()

            intCountRowEn = dsService.Tables("Result").Rows.Count - intCountRowTh
            '--------------------------------------------------------------------------------------------------
            'ทำการคิวรี ด้วย uniqueID

            'Dim tblSaction = From tblAmlList In SanctionEntity.AMLLIST_THAI Where tblAmlList.UNIQUEID = uniqueID1 _
            '                Select tblAmlList.UNIQUEID, tblAmlList.FIRSTNAME, tblAmlList.MIDDLENAME, _
            '                tblAmlList.LASTNAME, tblAmlList.CUSTTYPE, tblAmlList.FULLNAME, tblAmlList.COUNTRY, tblAmlList.DATASOURCE, _
            '                tblAmlList.LASTUPDATEDATE, SELECTLIST = ""


            'ds.Tables("Result").Merge(clsLINQToDataTable.LINQToDataTable(tblSaction, "Result"))


            'intCountRowUniqueID = ds.Tables("Result").Rows.Count - (intCountRowTh + intCountRowEn)
            'total = intCountRowTh + intCountRowEn + intCountRowUniqueID
            ds.Tables.Add(dsService.Tables("result").Copy)



            grdList.DataSource = ds.Tables("Result")
            grdList.DataBind()

            For iCountBegin As Integer = 0 To total - 1
                'If (iCountBegin >= 0 And iCountBegin < intCountRowTh) Then
                '    setGridRowBackground(grdList, iCountBegin, Drawing.Color.Honeydew)
                'End If
                'If (iCountBegin >= intCountRowTh And iCountBegin < (intCountRowTh + intCountRowEn)) Then
                '    setGridRowBackground(grdList, iCountBegin, Drawing.Color.PapayaWhip)
                'End If
                If (iCountBegin >= 0 And iCountBegin < intCountRowTh) Then
                    Dim arrColor() As Integer = {152, 251, 152}
                    'setGridRowBackground(grdList, iCountBegin, Drawing.Color.Honeydew)
                    setGridRowBackground(grdList, iCountBegin, arrColor)
                End If
                If (iCountBegin >= intCountRowTh And iCountBegin < (intCountRowTh + intCountRowEn)) Then
                    Dim arrColor() As Integer = {255, 222, 173}
                    'setGridRowBackground(grdList, iCountBegin, Drawing.Color.PapayaWhip)
                    setGridRowBackground(grdList, iCountBegin, arrColor)
                End If

            Next

        Catch ex As Exception
            WriteError(ex, "callWSVToChkName")
            Throw ex
        End Try
    End Sub

    Private Sub setGridRowBackground(ByVal Grid As GridView, ByVal intRow As Integer, ByVal colorBack As Drawing.Color)
        Try
            Grid.Rows(intRow).BackColor = colorBack
        Catch ex As Exception
            WriteError(ex, "setGridRowBackground")
            Throw ex
        End Try
    End Sub
    Private Sub setGridRowBackground(ByVal Grid As GridView, ByVal intRow As Integer, ByVal colorBack As Array)
        Try
            'Grid.Rows(intRow).BackColor = colorBack
            Grid.Rows(intRow).BackColor = Drawing.Color.FromArgb(colorBack(0), colorBack(1), colorBack(2))
        Catch ex As Exception
            WriteError(ex, "setGridRowBackground")
            Throw ex
        End Try
    End Sub

    Private Sub AutoInput()
        Try
            lblUniqueID.Text = uniqueID1
            lblTHName.Text = NameTh1
            lblENName.Text = NameEn1.ToUpper
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub QuerySanction()
        Try
            'Dim THName As String = NameTh1.Trim.Substring(0, NameTh1.Trim.Length / 2).Trim
            ' Dim ENName As String = NameEn1.Trim.Substring(0, NameEn1.Trim.Length / 2).ToUpper.Trim

            'Dim qryList = From A In ListEntity.MAMLLIST _
            '               Where A.AMLTYPE = 2 And
            '               (A.FIRSTNAME.ToUpper.Contains(NameEn1.ToUpper) Or A.FIRSTNAME.Contains(NameTh1) Or A.FULLNAME.ToUpper.Contains(NameEn1.ToUpper))
            '             Select A

            'Dim qryList = From A In ListEntity.MAMLLIST _
            '              Where A.AMLTYPE = 2 And A.ISACTIVE = True
            '            Select A

            'If Not NameTh1.Trim = "" And Not NameEn1.Trim = "" Then
            '    qryList = qryList.Where(Function(c) c.FIRSTNAME.Contains(NameTh1.Trim) Or c.FULLNAME.Contains(NameTh1.Trim) Or c.FIRSTNAME.ToUpper.Contains(NameEn1.Trim.ToUpper) Or c.FULLNAME.ToUpper.Contains(NameEn1.Trim.ToUpper))
            'ElseIf Not NameTh1.Trim = "" Then
            '    qryList = qryList.Where(Function(c) c.FIRSTNAME.Contains(NameTh1.Trim) Or c.FULLNAME.Contains(NameTh1.Trim))
            'ElseIf Not NameEn1.Trim = "" Then
            '    qryList = qryList.Where(Function(c) c.FIRSTNAME.ToUpper.Contains(NameEn1.Trim.ToUpper) Or c.FULLNAME.ToUpper.Contains(NameEn1.Trim.ToUpper))
            'End If


            'If ds.Tables.Contains("dsList") Then
            '    ds.Tables.Remove("dsList")
            'End If
            'ds.Tables.Add(clsDataTable.LINQToDataTable(qryList, "dsList").Copy)

            callWSVToChkName(NameTh1.Trim, "", "", _
                          NameEn1.Trim, "", "")




            'grdList.DataSource = ds.Tables("dsList")
            'grdList.DataBind()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Function checkBeforeSave() As Boolean
        Try
            Dim result As Boolean = False
            If rdoSanction.SelectedIndex = 0 Then
                Dim chk As CheckBox
                For i As Integer = 0 To grdList.Rows.Count - 1
                    chk = CType(grdList.Rows(i).FindControl("chkList"), CheckBox)
                    If chk.Checked = True Then
                        result = True
                    End If
                Next
            Else
                result = True
            End If

            Return result
        Catch ex As Exception
            WriteError(ex, "checkBeforeSave")
            Throw ex
        End Try
    End Function

    Private Sub saveData()
        Try
            Dim chk As CheckBox
            For i As Integer = 0 To grdList.Rows.Count - 1
                chk = CType(grdList.Rows(i).FindControl("chkList"), CheckBox)
                If chk.Checked = True Then
                    ds.Tables("Result").Rows(i)("SELECTLIST") = "0" 'ทำชั่วคราว รอเปลี่ยนฟิลด์อีกที
                    If grdList.Rows(i).Cells(3).Text = "PEPs" Then
                        If Session("Sanction") = Nothing Then
                            Session("Sanction") = "- พบรายชื่อใน PEPs List"
                        Else

                            Session("Sanction") = ", " + "PEPs"
                        End If
                    Else
                        If Session("Sanction") = Nothing Then
                            Session("Sanction") = "- พบรายชื่อใน " + grdList.Rows(i).Cells(3).Text
                        Else
                            Session("Sanction") = ", " + grdList.Rows(i).Cells(3).Text
                        End If
                    End If
                Else
                    ds.Tables("Result").Rows(i)("SELECTLIST") = "1" 'ทำชั่วคราว รอเปลี่ยนฟิลด์อีกที
                End If
                ds.Tables("Result").AcceptChanges()
            Next
        Catch ex As Exception
            WriteError(ex, "saveData")
            Throw ex
        End Try
    End Sub

#End Region

#Region "Event Button"

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try
            If checkBeforeSave() = True Then
                saveData()
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.close();", True)
            Else
                ScriptManager.RegisterStartupScript(btnOK, Page.GetType(), "alert", "alert('กรุณาเลือกรายชื่อในลิสต์')", True)
            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

#End Region

    Protected Sub rdoSanction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoSanction.SelectedIndexChanged
        If rdoSanction.SelectedIndex = 0 Then
            btnOK.Enabled = True
            grdList.Enabled = True
        Else
            btnOK.Enabled = True
            grdList.Enabled = False
        End If

    End Sub

    Protected Sub grdList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdList.RowDataBound
        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblActiveDate As Label = CType(e.Row.FindControl("lblActiveDate"), Label)
            If Not (lblActiveDate.Text = "") Then
                convertDate = New clsDateFormat
                lblActiveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblActiveDate.Text)
                convertDate = Nothing
            End If
            e.Row.Attributes.Add("onmouseover", "javascript:this.style.cursor = 'pointer';")
            e.Row.Attributes.Add("ondblclick", "win = window.showModalDialog('frmshowDetail.aspx?RowIndexDetail=" & e.Row.RowIndex & "',null,'width=600,height=800','_blank');")

        End If

    End Sub

    Public Sub WriteError(ByVal objEx As Exception, ByVal strFunctionName As String)
        Try

      
        Dim clsSessionMenuRight As clsSessionMenuRight = Session("clsMenuRight")
        Dim cls As New clsWriteError
        cls.GetstrErrorMessage = objEx.Message
        cls.GetstrErrorException = If(objEx.InnerException Is Nothing, "", objEx.InnerException.Message)
        cls.GetstrFromName = "AMLListBiz"
        cls.GetstrUserName = clsSessionMenuRight.getUserName
        cls.GetstrUserRight = clsSessionMenuRight.getUserRight
        cls.GetstrFunctionName = strFunctionName
        cls.CreateFileXml(clsSessionMenuRight.getBranchCode)
        clsSessionMenuRight = Nothing
            cls = Nothing
        Catch ex As Exception

        End Try
    End Sub

End Class
